package com.szholly.pro.watergas_server.ps.android.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ServletContextAware;

import com.szholly.pro.watergas_server.ps.android.bean.PageInfo;
import com.szholly.pro.watergas_server.ps.android.bean.PipeSearchParm;
import com.szholly.pro.watergas_server.ps.android.common.ResultUtils;
import com.szholly.pro.watergas_server.ps.android.common.ValidUtils;
import com.szholly.pro.watergas_server.ps.android.service.MoveDataService;
import com.szholly.data.general.RowBase;

@Controller
@RequestMapping("/android/pipe/*")
public class PipeControl implements ServletContextAware {
	@SuppressWarnings("unused")
	private ServletContext servletContext;

	@Override
	public void setServletContext(ServletContext arg0) {
		this.servletContext = arg0;
	}

	@RequestMapping("/getSplashInitData")
	@ResponseBody
	public String getSplashInitData() {
		try {
			List<RowBase> factoryList = MoveDataService
					.getFactoryList(null);
			List<RowBase> stationList = MoveDataService
					.getStationList(null,null);
			List<RowBase> parameterList = MoveDataService
					.getParameterList();
			List<RowBase> administrativeDivisionList = MoveDataService
					.getAdministrativeDivisionList();
			PipeService wdpService = PipeService.getInstance();

			Map<String, Object> resultMap = new HashMap<>();
			resultMap.put("factoryList", factoryList);
			resultMap.put("stationList", stationList);
			resultMap.put("parameterList", parameterList);
			resultMap.put("administrativeDivisionList",
					administrativeDivisionList);
			resultMap.put("projectList", wdpService.getProjectList());
			resultMap.put("projectItemList",
					wdpService.getProjectItemList());
			resultMap.put("pipeList", wdpService.getPipeList());

			List<RowBase> mapDomainList = new ArrayList<RowBase>();
			mapDomainList.addAll(wdpService.getUnitList());
			mapDomainList.addAll(wdpService.getStreetList());
			mapDomainList.addAll(wdpService.getTypeList());
			mapDomainList.addAll(wdpService.getChildTypeList());
			resultMap.put("mapDomainList", mapDomainList);
			return ResultUtils.getToastResult(resultMap);
		} catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}

	@RequestMapping("/getEndoscopicDefect")
	@ResponseBody
	public String getEndoscopicDefect(String projectId,
			String projectItemId, String pipeId, String defectId) {
		try {
			List<RowBase> list = PipeService.getInstance()
					.getEndoscopicDefectList(projectId, projectItemId,
							pipeId, defectId);
			return ResultUtils.getToastResult(list);
		} catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}

	@RequestMapping("/getEndoscopicReviewDetail")
	@ResponseBody
	public String getEndoscopicReviewDetail(String pipeCode,
			String pageIndex) {
		try {
			int pageIndexInt = Integer.parseInt(pageIndex);
			PipeService service = PipeService.INSTANCE;
			RowBase rowBase = service.getEndoscopicReviewDetail(
					pipeCode, pageIndexInt);
			return ResultUtils.getToastResult(rowBase);
		} catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}

	@RequestMapping("/getEndoscopicPrecision")
	@ResponseBody
	public String getEndoscopicPrecision(String projectCode) {
		try {
			PipeService service = PipeService.INSTANCE;
			List<RowBase> list = service
					.getEndoscopicPrecisionList(projectCode);
			return ResultUtils.getToastResult(list);
		} catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}

	// 排水管网列表分页查询查询
	@RequestMapping("/getSearchData")
	@ResponseBody
	public String getSearchData(String token, String userId,
			String typeName, String typeChildName, String unitName,
			String streetName, String searchText, int pageIndex,
			int pageSize) {
		try {
			ValidUtils.validateToken(token);
			ValidUtils.hasText(userId, "用户编号为空,搜索失败");
			ValidUtils.hasText(typeName, "管网类型为空,搜索失败");
			if (pageIndex<=0) {
				pageIndex =1;
			}			
			if (pageSize<=0) {
				pageSize=15;
			}
			PageInfo pageInfo = new PageInfo(pageIndex,pageSize);
			PipeSearchParm searchParm = new PipeSearchParm(unitName, streetName, typeChildName, searchText);
			Map<String, Object> map = PipeSearchService.INSTANCE.getSearchDataMap(typeName, searchParm, pageInfo);
			return ResultUtils.getToastResult(map);
		} catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}

}
